package com.sunnada.demo.controller;

import com.sunnada.demo.bean.Cat;
import com.sunnada.demo.service.CatService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/cat")
public class CatController {

    @Resource
    private CatService catService;

    @RequestMapping("/show")
    public String showCat() {
        return "cat";
    }

    @RequestMapping( "/getCat")
    @ResponseBody
    public Cat getById(String id) {
        Cat cat = catService.getById(id);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("cat");
        mv.addObject("cat", cat);
        catService.getById(id);
        return cat;
    }

    @RequestMapping("/getList")
    public ModelAndView getList() {
        List<Cat> catList = catService.getList();
        ModelAndView mv = new ModelAndView();
        mv.setViewName("cat");
        mv.addObject("catList", catList);
        return mv;
    }
}
